package com.example.micro.aop.login.controller;

import com.example.micro.aop.login.entity.User;
import com.example.micro.aop.login.entity.annotation.Authorized;
import com.example.micro.aop.login.service.UserService;
import com.example.micro.aop.login.utils.TokenUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RequestMapping("/user")
@RestController
public class LoginController {
    @Resource
    private UserService userService;


    @ApiOperation("用户登录接口")
    @PostMapping("/login")
    public String login(@ApiParam("用户登录信息") @RequestBody User user) {
        User userForBase = userService.getUserByName(user.getName(), user.getPassword());
        if (userForBase == null) {
            return "账号验证错误！请重新输入!";
        } else {
            return TokenUtil.getToken(userForBase);
        }
    }


    @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token标记", required = true)
    @ApiOperation("获取用户信息进行JWT验证")
    @Authorized
    @GetMapping("/getMessage")
    public String getMessage() {
        return ("你已经通过验证");
    }
}

